package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.R;
import com.itheima.common.SnowflakeUtils;
import com.itheima.mapper.backend.CategoryDao;
import com.itheima.pojo.backend.Category;
import com.itheima.pojo.backend.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SnowflakeUtils snowflakeUtils;


    /**
     * 添加
     * @param request
     * @param category
     * @return
     */
    public R add(HttpServletRequest request, Category category){

        List<Category> exist = categoryDao.isExist(category);
        if (exist.size()!= 0) {
            return R.error(category.getName()+"分类名'已存在");
        }

        //用雪花算法获取id
        category.setId(String.valueOf(snowflakeUtils.snowflakeId()));

        //获取创建人
        String token = request.getHeader("Authorization");
        String s = stringRedisTemplate.opsForValue().get(token);
        category.setCreateUser(s);
        category.setUpdateUser(s);

        //获取当前时间
        long l = System.currentTimeMillis();
        category.setCreateTime(String.valueOf(l));
        category.setUpdateTime(String.valueOf(l));
        int add = categoryDao.add(category);
        if (add>0){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }
    }
    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    public R select(Integer page, Integer pageSize, String name) {
        //构建分页拦截器
        PageHelper.startPage(page, pageSize);

        List<Category> employeeList = categoryDao.select(name);

        PageInfo<Category> PageInfo = new PageInfo<>(employeeList);

        //返回值对象，这里的P类是根据前端要的数据专门写的类，。属性只有这四条
        Paging p = new Paging();
        //总条数
        p.setTotal(PageInfo.getTotal());
        //每页显示条数
        p.setSize(PageInfo.getSize());
        //当前页码
        p.setCurrent(PageInfo.getPageNum());
        //被展示的数据集
        p.setRecords(employeeList);
        return R.success(p);
    }


    public R list(){
        List<Category> list = categoryDao.list();
        if (list!=null){
            return R.success(list);
        }else {
            return R.error("暂无数据");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public R delete(String id){
        int delete = categoryDao.delete(id);
        if (delete>0){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    /**
     * 修改
     * @param request
     * @param category
     * @return
     */
    public R updatCategory(HttpServletRequest request, Category category){
        long l = System.currentTimeMillis();
        category.setUpdateTime(String.valueOf(l));

        String token = request.getHeader("Authorization");
        String s = stringRedisTemplate.opsForValue().get(token);
        System.out.println(s);
        category.setUpdateUser(s);
        List<Category> exist = categoryDao.isExist(category);
        if (exist.size() != 0) {
            return R.error(category.getName() + "分类名已存在");
        }

        int i = categoryDao.updatCategory(category);
        if (i > 0) {
            return R.success("修改分类信息成功");
        } else {
            return R.error("修改分类信息失败");
        }
    }

}
